using UnityEngine;
using UnityEngine.UI;

public class KeyboardSetup : MonoBehaviour
{
    public InputManager inputManager;  // 输入管理器的引用

    void Start()
    {
        // 如果没有分配InputManager，尝试查找
        if (inputManager == null)
        {
            inputManager = FindObjectOfType<InputManager>();
        }
        
        SetupAllButtons();
    }

    void SetupAllButtons()
    {
        // 获取所有按钮
        Button[] buttons = GetComponentsInChildren<Button>();

        foreach (Button button in buttons)
        {
            Text buttonText = button.GetComponentInChildren<Text>();
            if (buttonText != null)
            {
                string buttonLabel = buttonText.text.ToUpper();
                
                // 如果是退格键
                if (buttonLabel == "BACK" || buttonLabel == "DELETE" || buttonLabel == "←")
                {
                    button.onClick.AddListener(() => inputManager.OnBackspaceClick());
                }
                // 如果是字母按钮
                else if (buttonLabel.Length == 1 && char.IsLetter(buttonLabel[0]))
                {
                    string letter = buttonLabel;
                    button.onClick.AddListener(() => inputManager.OnLetterButtonClick(letter));
                }
            }
        }
    }
} 